package cn.stronglink.oil.utils;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import com.loopj.android.http.SyncHttpClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Console;
import java.io.UnsupportedEncodingException;

import cn.stronglink.oil.MyApplication;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicHeader;

/**
 * Created by tgb on 2017/4/25 16:17
 */

public class HttpClientUtils {
//    public static String HOST="10.103.0.202:8080";
    public static String HOST="123.206.34.157";
    public static String CAMERA_HOST = "39.100.153.242:1935";
    private final static String URL_GET_GAS_TYPE = "http://www.yuzhantao.com:8080/web-oil/api/mobile/getGasTypeList";
    private static String URL_LOGIN = "http://"+HOST+"/api/mobile/joinRoom.jhtml";
    private final static String URL_GET_GAS_HISTORY_LIST = "http://www.yuzhantao.com:8080/web-oil/api/mobile/getGasHistoryList";
    private final static int TIME_OUT = 20000;
    public static String URL_MOBILE = "http://"+HOST+"/api/mobile/index.jhtml";
//    private static String URL_Video_List = "http://" + HOST + "/api/mobile/qryCameraSteam.jhtml";
    private static String URL_Video_List = "http://" + HOST + "/webApi/camera/qryCameraRemote.jhtml";

    public static void setHOST(String HOST) {
        HttpClientUtils.HOST = HOST;
        URL_LOGIN = "http://"+HOST+"/api/mobile/joinRoom.jhtml";
        URL_MOBILE = "http://"+HOST+"/api/mobile/index.jhtml";
        URL_Video_List = "http://" + HOST + "/webApi/camera/qryCameraRemote.jhtml";

    }

    public static void setCameraHost(String cameraHost){
        HttpClientUtils.CAMERA_HOST = cameraHost;
    }

    /**
     * 登录接口
     *
     * @param param
     * @param responseHandler
     */
    public static void login(String userName,String param, ResponseHandlerInterface responseHandler) throws Exception {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
//        RequestParams params = new RequestParams();
//        params.put("account",userName);
//        params.put("roomCode", param);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account",userName);
        jsonObject.put("roomCode",param);
        StringEntity entity = new StringEntity(jsonObject.toString());
        asyncHttpClient.setMaxRetriesAndTimeout(1, TIME_OUT);
        asyncHttpClient.post(MyApplication.getInstance(),URL_LOGIN,entity, "application/json",responseHandler);
    }

    /**
     * 获取历史数据
     *
     * @param controlDeviceSerial
     * @param pageOffset
     * @param pageSize
     */
    public static void getGasHistoryList(String controlDeviceSerial, int gasType, int pageOffset, int pageSize, ResponseHandlerInterface responseHandler) {
        AsyncHttpClient syncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("controlDeviceSerial", controlDeviceSerial);
        params.put("gasType", gasType);
        params.put("pageOffset", pageOffset);
        params.put("pageSize", pageSize);
        syncHttpClient.setMaxRetriesAndTimeout(1, TIME_OUT);
        syncHttpClient.post(URL_GET_GAS_HISTORY_LIST, params, responseHandler);
    }

    /**
     * 获取历史数据
     *
     * @param controlDeviceSerial
     * @param pageOffset
     * @param pageSize
     */
    public static void getGasHistoryList(String controlDeviceSerial, int pageOffset, int pageSize, ResponseHandlerInterface responseHandler) {
        AsyncHttpClient syncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("controlDeviceSerial", controlDeviceSerial);
        params.put("pageOffset", pageOffset);
        params.put("pageSize", pageSize);
        syncHttpClient.setMaxRetriesAndTimeout(1, TIME_OUT);
        syncHttpClient.post(URL_GET_GAS_HISTORY_LIST, params, responseHandler);
    }

    /**
     * 获取气体类型
     * @param responseHandler
     */
    public static void getGasType(ResponseHandlerInterface responseHandler){
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.setMaxRetriesAndTimeout(1, TIME_OUT);
        asyncHttpClient.get(URL_GET_GAS_TYPE,responseHandler);
    }

    /**
     * 获取气体类型
     * @param responseHandler
     */
    public static void getVideoUrlList(String param,ResponseHandlerInterface responseHandler){
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
//        RequestParams params = new RequestParams();
//        params.put("");
//        Log.e("getVideoUrlList: ", param);
        asyncHttpClient.setMaxRetriesAndTimeout(1, TIME_OUT);
        asyncHttpClient.post(URL_Video_List,responseHandler);
    }


}
